home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WBITVEC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-07  |  8.6 KB  |  444 lines

  1. #include "VecLocal.h"
  2. #pragma hdrstop
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5.  
  6. //.parse
  7.  
  8. BitVector BitVector::operator|(const BitVector& BV) const
  9.   {
  10.     BitVector V=*this;
  11.     V|=BV;
  12.     return V;
  13.   }
  14.  
  15. //.parse
  16.  
  17. void BitVector::operator|=(const BitVector& BV)
  18.   {
  19.     if (BV.Len>Alloc) ReAlloc((BV.Len+Extra)*8);
  20.     long MinBytes=Min(BV.Len,Len);
  21.     long I;
  22.     For(I,MinBytes)
  23.       {
  24.         Byte huge* B=PtrInc(P,I);
  25.         int X=(*B);
  26.         X|=int(*PtrInc(BV.P,I));
  27.         *B=Byte(X);
  28.       }
  29.     if (BV.Len>Len)
  30.       {
  31.         Len=BV.Len;
  32.         for(I=MinBytes;I<Len;I++) (*PtrInc(P,I))=*PtrInc(BV.P,I);
  33.       }
  34.     BitLen=Max(BitLen,BV.BitLen);
  35.   }
  36.  
  37. //.parse
  38.  
  39. Bool BitSetRef::operator=(Bool b)
  40.   {
  41.     #ifdef BorlandFixedByteMath
  42.       if (b) *P |= M;
  43.       else *P &= ~M;
  44.       return b;
  45.     #else
  46.       int I=*P;
  47.       if (b) I |= int(M);
  48.       else I &= (255-int(M));
  49.       *P=Byte(I);
  50.       return b;
  51.     #endif
  52.   }
  53.  
  54. //.parse
  55.  
  56. Bool BitRef::operator=(Bool b)
  57.   {
  58.     #ifdef BorlandFixedByteMath
  59.       if (b) *P |= M;
  60.       else *P &= ~M;
  61.       return b;
  62.     #else
  63.       int I=*P;
  64.       if (b) I |= int(M);
  65.       else I &= (255-int(M));
  66.       *P=Byte(I);
  67.       return b;
  68.     #endif
  69.   }
  70.  
  71. //.parse
  72.  
  73. void QSB(Byte* P, int Index, Bool b)
  74.   {
  75.     P+=size_t(Index/8);
  76.     Byte M=Byte(1<<int(Index%8));
  77.     #ifdef BorlandFixedByteMath
  78.       if (b) *P |= M;
  79.       else *P &= ~M;
  80.     #else
  81.       int I=*P;
  82.       if (b) I |= int(M);
  83.       else I &= (255-int(M));
  84.       *P=Byte(I);
  85.     #endif
  86.   }
  87.  
  88. //.parse
  89.  
  90. BitRef::BitRef(Byte huge* BP, long BitOffset)
  91.   {
  92.     P=PtrInc(BP,BitOffset/8);
  93.     M=Byte(1<<int(BitOffset%8));
  94.   }
  95.  
  96. //.parse
  97.  
  98. void SetBitRange(void* BitPointer, long Index1,long Index2,Bool X)
  99.   {
  100.     Byte* P=(Byte*)BitPointer;
  101.     if (Index1>Index2) return;
  102.     long ByteIndex1=Index1/8;
  103.     long ByteIndex2=Index2/8;
  104.     Byte XByte=0;
  105.     if (X)
  106.       {
  107.         X=1;
  108.         XByte=255;
  109.       }
  110.     if (ByteIndex2>ByteIndex1+1)
  111.       {
  112.         long I;
  113.         for(I=ByteIndex1+1;I<ByteIndex2;I++) *PtrInc(P,I)=XByte;
  114.       }
  115.     if (ByteIndex1==ByteIndex2)
  116.       {
  117.         Byte B1=Byte(Index1%8);
  118.         Byte B2=Byte(Index2%8);
  119.         Byte I;
  120.         Byte huge* B=PtrInc(P,ByteIndex1);
  121.         Byte Z=0;
  122.         for(I=B1;I<=B2;I++) Z|=(1<<I);
  123.         if (X) *B|=Z;
  124.         else *B&=~Z;
  125.       }
  126.     else
  127.       {
  128.         Byte B=Byte(Index1%8);  // the first bit num to set
  129.         if (B==0) *PtrInc(P,ByteIndex1)=XByte;  // set all the bits of this byte
  130.         else
  131.           {
  132.             B=Byte(1<<B); // now B is a mask for the bit
  133.             B--; // now B is a mask for all the previous bits
  134.             Byte huge* BP=PtrInc(P,ByteIndex1);
  135.             if (X) (*BP)|=~B;
  136.             else (*BP)&=B; // all of the unmasked bits are cleared
  137.           }
  138.         B=Byte(Index2%8);  // the last bit num to clear
  139.         if (B==7) *PtrInc(P,ByteIndex2)=XByte;  // set all the bits of this byte
  140.         else
  141.           {
  142.             B=Byte(1<<(B+1)); // now B is a mask for the next bit
  143.             B--; // now B is a mask for all the bits we want to clear
  144.             Byte huge* BP=PtrInc(P,ByteIndex2);
  145.             if (X) (*BP)|=B;
  146.             else (*BP)&=~B; // all of the masked bits are cleared
  147.           }
  148.       }
  149.   }
  150.  
  151. //.parse
  152.  
  153. BitVector::BitVector():ByteVector()
  154.   {
  155.     BitLen=0;
  156.     Word I;
  157.     For(I,Alloc) P[I]=0;
  158.   }
  159.  
  160. //.parse
  161.  
  162. BitVector::BitVector(long I0,long I1):ByteVector()
  163.   {
  164.     long W[2];
  165.     W[0]=I0;
  166.     W[1]=I1;
  167.     CtorsHelper(W,2);
  168.   }
  169.  
  170. //.parse
  171.  
  172. BitVector::BitVector(long I0,long I1,long I2):ByteVector()
  173.   {
  174.     long W[3];
  175.     W[0]=I0;
  176.     W[1]=I1;
  177.     W[2]=I2;
  178.     CtorsHelper(W,3);
  179.   }
  180.  
  181.  
  182. //.parse
  183.  
  184. BitVector::BitVector(long I0,long I1,long I2,long I3):ByteVector()
  185.   {
  186.     long W[4];
  187.     W[0]=I0;
  188.     W[1]=I1;
  189.     W[2]=I2;
  190.     W[3]=I3;
  191.     CtorsHelper(W,4);
  192.   }
  193.  
  194. //.parse
  195.  
  196. BitVector::BitVector(long I0,long I1,long I2,long I3,long I4):ByteVector()
  197.   {
  198.     long W[5];
  199.     W[0]=I0;
  200.     W[1]=I1;
  201.     W[2]=I2;
  202.     W[3]=I3;
  203.     W[4]=I4;
  204.     CtorsHelper(W,5);
  205.   }
  206.  
  207. //.parse
  208.  
  209. BitVector::BitVector(long I0,long I1,long I2,long I3,long I4,long I5):
  210.       ByteVector()
  211.   {
  212.     long W[6];
  213.     W[0]=I0;
  214.     W[1]=I1;
  215.     W[2]=I2;
  216.     W[3]=I3;
  217.     W[4]=I4;
  218.     W[5]=I5;
  219.     CtorsHelper(W,6);
  220.   }
  221.  
  222. //.parse
  223.  
  224. BitVector::BitVector(long I0,long I1,long I2,long I3,long I4,long I5,long I6):
  225.       ByteVector()
  226.   {
  227.     long W[7];
  228.     W[0]=I0;
  229.     W[1]=I1;
  230.     W[2]=I2;
  231.     W[3]=I3;
  232.     W[4]=I4;
  233.     W[5]=I5;
  234.     W[6]=I6;
  235.     CtorsHelper(W,7);
  236.   }
  237.  
  238. //.parse
  239.  
  240. BitVector::BitVector(long I0,long I1,long I2,long I3,long I4,long I5,long I6,long I7):
  241.       ByteVector()
  242.   {
  243.     long W[8];
  244.     W[0]=I0;
  245.     W[1]=I1;
  246.     W[2]=I2;
  247.     W[3]=I3;
  248.     W[4]=I4;
  249.     W[5]=I5;
  250.     W[6]=I6;
  251.     W[7]=I7;
  252.     CtorsHelper(W,8);
  253.   }
  254.  
  255. //.parse
  256.  
  257. BitVector::BitVector(long I0,long I1,long I2,long I3,long I4,long I5,long I6,long I7,long I8):
  258.       ByteVector()
  259.   {
  260.     long W[9];
  261.     W[0]=I0;
  262.     W[1]=I1;
  263.     W[2]=I2;
  264.     W[3]=I3;
  265.     W[4]=I4;
  266.     W[5]=I5;
  267.     W[6]=I6;
  268.     W[7]=I7;
  269.     W[8]=I8;
  270.     CtorsHelper(W,9);
  271.   }
  272.  
  273. //.parse
  274.  
  275. BitVector::BitVector(long I0):ByteVector()
  276.   {
  277.     long& W=long(I0);
  278.     CtorsHelper(&W,1);
  279.   }
  280.  
  281. //.parse
  282.  
  283. BitVector::BitVector(const BitVector& BV):ByteVector(BV)
  284.   {
  285.     BitLen=BV.BitLen;
  286.     long I;
  287.     for(I=Len;I<Alloc;I++) *PtrInc(P,I)=0;
  288.   }
  289.  
  290. //.parse
  291.  
  292. void BitVector::ReAlloc(long NewCapacity)
  293.   {
  294.     long BN=BytesNeeded(NewCapacity);
  295.     ByteVector::ReAlloc(BN);
  296.     long I;
  297.     for(I=Len;I<Alloc;I++) *PtrInc(P,I)=0;
  298.   }
  299.  
  300. //.parse
  301.  
  302. static long* LongPtrInc(const long* P, long I)
  303.   {
  304.     return (long*)PtrInc((Byte*)P,I*4L);
  305.   }
  306.  
  307. void BitVector::CtorsHelper(const long* LP, long Length)
  308.   {
  309.     BitLen=0;
  310.     long I;
  311.     For(I,Length) BitLen=Max(*LongPtrInc(LP,I),BitLen);
  312.     BitLen++;
  313.     Len=BytesNeeded(BitLen);
  314.     if (Len>Alloc) ReAlloc(BitLen+Extra);
  315.     Clear();
  316.     For(I,Length) Ref(*LongPtrInc(LP,I))=On;
  317.   }
  318.  
  319. //.parse
  320.  
  321. void ClearBitRange(void* P, long Index1,long Index2)
  322.   {
  323.     SetBitRange(P,Index1,Index2,0);
  324.   }
  325.  
  326. //.parse
  327.  
  328. void BitVector::ClearRange(long Index1,long Index2)
  329.   {
  330.     SetRange(Index1,Index2,0);
  331.   }
  332.  
  333. //.parse
  334.  
  335. void BitVector::SetRange(long Index1,long Index2,Bool X)
  336.   {
  337.     if (Index1>Index2) return;
  338.     if (Index2>=BitLen) Ref(Index2);
  339.     SetBitRange(P,Index1,Index2,X);
  340.   }
  341.  
  342. //.parse
  343.  
  344. void BitVector::operator=(const BitVector& BV)
  345.   {
  346.     Assign(BV);
  347.     BitLen=BV.BitLen;
  348.     long I;
  349.     for(I=Len;I<Alloc;I++) *PtrInc(P,I)=0;
  350.   }
  351.  
  352. //.parse
  353.  
  354. void BitVector::operator&=(const BitVector& BV)
  355.   {
  356.     long MinBytes=Min(BV.Len,Len);
  357.     long I;
  358.     For(I,MinBytes) (*PtrInc(P,I))&=*PtrInc(BV.P,I);
  359.     if (BV.BitLen<BitLen)
  360.       {
  361.         for (I=BV.Len;I<Len;I++) *PtrInc(P,I)=0;
  362.         Len=BV.Len;
  363.         BitLen=BV.BitLen;
  364.         if (Len>0)
  365.           {
  366.             Byte BitsToKill=Byte(7-int(BitLen%8));
  367.             Byte huge* B=PtrInc(P,Len-1);
  368.             *B<<=BitsToKill;
  369.             *B>>=BitsToKill;
  370.           }
  371.       }
  372.   }
  373.  
  374. //.parse
  375.  
  376. BitVector::operator String() const
  377.   {
  378.     Word Stop=Word(Min(long(BitLen),long(65500L))); // the max for a malloc
  379.     String S('0',Stop);
  380.     Word I;
  381.     For(I,Stop)
  382.         if (At(I)) S[I]='1';
  383.     return S;
  384.   }
  385.  
  386. //.parse
  387.  
  388. #define BitVecRefGuts                                  \
  389.     long BN=BytesNeeded(Index+1);                      \
  390.     if (BN>Alloc) ReAlloc(Index+DefaultVectorExtra);   \
  391.     if (Index>=BitLen)                                 \
  392.       {                                                \
  393.         BitLen=Index+1;                                \
  394.         Len=BN;                                        \
  395.       }                                                \
  396.     return BitRef(P,Index);
  397.  
  398. BitRef BitVector::Ref(long Index)
  399.   {
  400.     BitVecRefGuts
  401.   }
  402.  
  403. BitRef BitVector::operator[](long Index)
  404.   {
  405.     BitVecRefGuts
  406.   }
  407.  
  408. //.parse
  409.  
  410. Bool BitVector::At(long Index) const
  411.   {
  412.     if (Index>BitLen) return False;
  413.     else return (( (*PtrInc(P,Index/8)) & (1<<(Index%8)) )!=0);
  414.   }
  415.  
  416. //.parse
  417.  
  418. BitVector BitVector::At(long Index,long Length) const
  419.   {
  420.     BitVector BV;
  421.     BV.ReAlloc(Length);
  422.     long I;
  423.     For(I,Length)
  424.         if ((*this)(Index+I)) BV[I]=1;
  425.     return BV;
  426.   }
  427.  
  428. //.parse
  429.  
  430. BitVector BitVector::From(long Index) const
  431.   {
  432.     long Length=(Index>BitLen)?0:BitLen-Index;
  433.     return At(Index,Length);
  434.   }
  435.  
  436. //.parse
  437.  
  438. BitVector BitVector::After(long Index) const
  439.   {
  440.     long Length=(Index>=BitLen)?0:BitLen-Index-1;
  441.     return At(Index+1,Length);
  442.   }
  443.  
  444.